import json
import re
from latex2sympy2 import latex2sympy


def convert_to_float(s):
    s = s.strip()
    try:
        return float(s)
    except:
        try:
            return float(latex2sympy(s))
        except:
            try:
                s = re.sub(r'\\boxed{(.*)}', r'\1', s, flags=re.DOTALL)
                return float(latex2sympy(s))
            except:
                try:
                    s = re.sub(r'\boxed{(.*)}', r'\1', s, flags=re.DOTALL)
                    return float(latex2sympy(s))
                except:
                    return None
    
def calculate_accuracy(file_path):
    correct_count = 0
    total = 0
    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            try:
                data = json.loads(line)
                answer_str = data['answer']
                qwen_answer_str = data['qwen2.5-3b-instruct-answer']
                
                # 转换为浮点数
                answer = convert_to_float(answer_str)
                qwen_answer = convert_to_float(qwen_answer_str)
                
                # 计算差值
                if abs(answer - qwen_answer) < 1e-3:
                    correct_count += 1
                total += 1
            except:
                print(f"处理行时出错: {line}")
                total += 1
    if total == 0:
        return 0.0
    return correct_count / total

# 示例使用
file_path = '/home/work_nfs15/znlin/osum-bench/code/tgt_t2t/qwen2.5-3b-instruct/math401.json.t.r'
accuracy = calculate_accuracy(file_path)
print(f"准确度为: {accuracy:.4f}")